package com.xyd.Service;

import com.xyd.bean.Order;
import com.xyd.dao.NewsMapper;
import com.xyd.dao.OrderMapper;
import com.xyd.dao.ShoppingCarMapper;
import com.xyd.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class OrderService {
    SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
    OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
    ShoppingCarMapper shoppingCarMapper = sqlSession.getMapper(ShoppingCarMapper.class);
    public List<Order> getAll(){
        return orderMapper.getAll();
    }

    public double getCost(int id){
        return shoppingCarMapper.getCost(id);
    }

    public int addOrder(Order order){
        int i=0;
        int delResult=0;
        try {
           i =  orderMapper.insertAll(order);
           delResult = shoppingCarMapper.delShoppingByuid(order.getUserid());
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
        }
        if ( i >0 && delResult >0){
            return i;
        }
       return 0;
    }
    public int delOrder(int id){
        return orderMapper.delById(id);
    }
    public Order getById(int id){
        return orderMapper.getById(id);
    }
    public int modefiyOrder(Order order){
        return orderMapper.updateByid(order);
    }
    public List<Order> getByName(String username){
        return orderMapper.getByName(username);
    }
}
